<?php
if ( !defined("SCRIPT_DIR") ) {
	define("SCRIPT_DIR", dirname(__FILE__));
}
$path = explode('/',__FILE__);

if ( !defined("FILE") ) {
	define("FILE",end($path));
}

function path_format($file) {
	return SCRIPT_DIR . '/' . $file;
}

include_once(path_format('logger.php'));
include_once(path_format('config.php'));
include_once(path_format('mysql.php'));
include_once(path_format('redis.php'));

class SyncConf {
	public static $sync_sec = 5;//回写数据库时间
	public static $hlen = 20;
	public static $logout_diff = 604800; //86400秒 = 一天， logout_diff时间内无活动删除redis  857019
	public static $begin = 0;
	public static $end = 0;//
}

function sync(){
	$cache_redis = new RedisDriver();
    $ret = $cache_redis->initDataRedis(5);
    if (!$ret) {
	Config::$log->error(FILE, "initCacheRedis error");
    }
	$mysql = new MysqlDriver ();
	$rets = $mysql->connect ( Config::$mysql_config );
	if (! $rets) {
		Config::$log->error ( FILE, "connect mysql error." );
	}
	
	//把redis vip数据回写数据库
	$vipinfo = $cache_redis->data_redis->keys("hvip:*");
	if($vipinfo){
		foreach($vipinfo as $key=>$val){
			$row = $cache_redis->data_redis->hGetAll($val);
			$sql = 'select id from vipdays where uid='.$row['uid'];
			$result0 = $mysql->query($sql);
			$row0 = mysql_fetch_assoc($result0);
			$data = array(
				    'uid'=>$row['uid'],
					'level'=>$row['level'],
					'days'=>$row['days'],
					'vip_time'=>$row['vip_time'],
                    );
			if(empty($row0)){
				$mysql->insert('vipdays',$data);
			}else{
				$mysql->update('vipdays',$data,array('id'=>$row0['id']));
			}
	    }
	}

	//把专属房redis写入数据库
	$ExclusiveRoom = $cache_redis->data_redis->keys("hExclusiveRoom:*");
	if($ExclusiveRoom){
		foreach($ExclusiveRoom as $key=>$val){
			$row_s = $cache_redis->data_redis->hGetAll($val);
			$sql = 'select id from exclusive_room where ownerID='.$row_s['ownerID'];
			$result_s = $mysql->query($sql);
			$rows_s = mysql_fetch_assoc($result_s);
			$data_s = array(
			    'deskCode'=>$row_s['deskCode'],
				'ownerID'=>$row_s['ownerID'],
				'ownerName'=>$row_s['ownerName'],
				'deskDesc'=>$row_s['deskDesc'],
                'deskName'=>$row_s['deskName'],
                'gameConfig'=>$row_s['gameConfig'],
                'basePoint'=>$row_s['basePoint'],
                'playCount'=>$row_s['playCount'],
                'chip'=>$row_s['chip'],
                'deposit'=>$row_s['deposit'],
                'createTime'=>$row_s['createTime'],
                'isWork'=>$row_s['isWork']
			);
			if(empty($rows_s)){
				$mysql->insert('exclusive_room',$data_s);
			}else{
				$mysql->update('exclusive_room',$data_s,array('id'=>$rows_s['id']));
			}
		}
	}
	
    $cache_redis->deinitDataRedis ();
	$mysql->close ();

}

function main() {
		while(1) {
		sync();	
		echo date('Y:m:d H:i:s')." ZjhSyncDumpVipData working...\r\n";
		$sync_sec = SyncConf::$sync_sec;
		sleep($sync_sec);
		
	   }
}

main ();



?>